/**
 * 初始化质控规则详情对话框
 */
var RuleInfoDlg = {
    RuleData:  {},
    validateFields: {
        // expCode: {
        //     validators: {
        //         notEmpty: {
        //             message: '规则代码不能为空'
        //         }
        //     }
        // },
        ruleDesc: {
            validators: {
                notEmpty: {
                    message: '规则描述不能为空'
                }
            }
        }

    }
};



RuleInfoDlg.set = function(key, value) {
    this.RuleData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

RuleInfoDlg.get = function(key) {
    return $("#" + key).val();
}
//判断规则代码是否重复
RuleInfoDlg.codechange=function (){
    $.ajax({
        url: Sys.backendPath + "/sdc/contraindicationitemrule/isRepeat",
        dataType: "json",
        type: "post",
        traditional:true,
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        data: {'expCode':Common_GetValue('expCode')},
        success:function(res){
            if (res.code==500){
                $('#codeRepeatErr').css('display','block');
            }else {
                $('#codeRepeatErr').css('display','none');
            }
        },
        error:function(data){
            Sys.error("操作异常!" + data.responseJSON.message + "!");
        },
    });


}
//选择数据源后，查询对应的取值字段
RuleInfoDlg.sourceChange1 = function(source) {

    console.info("获取数据源字段1")
    var sourceId = source.value;
    $('#dataSourceItem1').combobox("clear");

    var ajax = new $ax(Sys.backendPath + "/sdc/contraindicationitemrule/getSourceFieldAndStandDic", function(res){
        var fieldMap = $.parseJSON(res.fieldMap);
        $('#dataSourceItem1').combobox({
            data: fieldMap,
            valueField:'value',
            textField:'text',
            onLoadSuccess: function () {
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        Sys.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set({"sourceId":sourceId});
    ajax.setAsync(false);
    ajax.start();
}

RuleInfoDlg.sourceChange2 = function(source) {

    console.info("获取数据源字段2")
    var sourceId = source.value;
    $('#dataSourceItem2').combobox("clear");

    var ajax = new $ax(Sys.backendPath + "/sdc/contraindicationitemrule/getSourceFieldAndStandDic", function(res){
        var fieldMap = $.parseJSON(res.fieldMap);
        $('#dataSourceItem2').combobox({
            data: fieldMap,
            valueField:'value',
            textField:'text',
            onLoadSuccess: function () {
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        Sys.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set({"sourceId":sourceId});
    ajax.setAsync(false);
    ajax.start();
}
//填充规则数据
RuleInfoDlg.initSourceForm = function (){
    var params = {};


    if ($("#id").val()==null||$("#id").val()=="") {
        return false;
    }else {
        params.id=$("#id").val();
        var ajax = new $ax(Sys.backendPath + "/sdc/contraindicationitemrule/getRule", function(data){
            $("#id").val(data.id);
            $("#dataSource1").combobox("select", data.dataSource1)
            $("#dataSource2").combobox("select", data.dataSource2);
            $("#dataSourceItem1").combobox("select", data.dataSourceItem1);
            $("#dataSourceItem2").combobox("select", data.dataSourceItem2);
            $("#param1").val(data.param1);
            $("#param2").val(data.param2);
            $("#operator1").val(data.operator1);
            $("#operator2").val(data.operator2);

            $("#expCode").val(data.expCode);
            $("#ruleDesc").val(data.ruleDesc);
            $("#ruleMemo").val(data.ruleMemo);
            $("#ruleType").combobox("select", data.ruleType);
            $("#defaultValue").combobox("select", data.defaultValue);
        },function (data){
        });
        ajax.setData(params);
        ajax.start();
    }
}


RuleInfoDlg.collectData = function () {
    this.set('dataSource1',$('#dataSource1').combobox('getValue')).set('dataSourceItem1',$('#dataSourceItem1').combobox('getValue'))
        .set('dataSource2',$('#dataSource2').combobox('getValue')).set('dataSourceItem2',$('#dataSourceItem2').combobox('getValue'))
        .set('param1',encodeURIComponent($('#param1').val())).set('ruleMemo',$('#ruleMemo').val())
        .set('param2',encodeURIComponent($('#param2').val())).set('operator1',encodeURIComponent($('#operator1').val()))
        .set('operator2',encodeURIComponent($('#operator2').val()))
        .set('ruleDesc',$('#ruleDesc').val()).set('expCode',$("#expCode").val()).set('id',$("#id").val());
};


/**
 * 关闭此对话框
 */
RuleInfoDlg.close = function() {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //先得到当前iframe层的索引,再执行关闭
}

/**
 * 验证数据是否为空
 */
RuleInfoDlg.validate = function () {
    $('#RuleInfoForm').data("bootstrapValidator").resetForm();
    $('#RuleInfoForm').bootstrapValidator('validate');
    return $("#RuleInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 保存
 */
RuleInfoDlg.addSubmit = function() {
    if (!this.validate()) {
        return;
    }
    var paramData = {};
    var contraindicationitemruleId=$("#id").val();
    if (contraindicationitemruleId==null||contraindicationitemruleId==""){
        paramData['id'] =null;
    }else {
        paramData['id']=contraindicationitemruleId;
    }

    var ajax = new $ax(Sys.backendPath + "/sdc/contraindicationitemrule/save", function (data) {

        Sys.success("保存成功!");
        RuleInfoDlg.close();
        window.parent.Obj.getItemRule();
    }, function (data) {
        Sys.error("保存失败!" + data.responseJSON.message + "!");
    });


    paramData['expCode'] = Common_GetValue('expCode');
    paramData['ruleDesc'] = Common_GetValue('ruleDesc');
    paramData['ruleMemo'] = Common_GetValue('ruleMemo');
    paramData['ruleType'] = Common_GetValue('ruleType');
    paramData['dataSource1'] = Common_GetValue('dataSource1');
    paramData['dataSource2'] = Common_GetValue('dataSource2');
    paramData['dataSourceItem1'] = Common_GetValue('dataSourceItem1');
    paramData['dataSourceItem2'] = Common_GetValue('dataSourceItem2');
    paramData['param1'] =Common_GetValue('param1');
    paramData['param2'] = Common_GetValue('param2');
    paramData['operator1'] = Common_GetValue('operator1');
    paramData['operator2'] = Common_GetValue('operator2');
    paramData['defaultValue'] = Common_GetValue('defaultValue');
    console.info(paramData)
    ajax.setData(paramData);
    ajax.start();
}


function Common_GetValue() {
    var itmValue = '';

    if (typeof(arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#'+ arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    }else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    }else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    }else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    }else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    }else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    }else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.checkbox('getValue');
    }else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    }else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}



$(function() {
    Sys.initValidator("RuleInfoForm", RuleInfoDlg.validateFields);
    $HUI.combobox('#ruleType',
        {
            data:[
                {'ruleType':'Day','Desc':"天数"},
                {'ruleType':'Hour','Desc':"小时"},
                {'ruleType':'Minute','Desc':"分钟"},
                {'ruleType':'String','Desc':"字符串"}
            ],
            valueField:'ruleType',
            textField:'Desc',
        })
    $HUI.combobox('#defaultValue',
        {
            data:[
                {'defaultValue':'0','Desc':"false"},
                {'defaultValue':'1','Desc':"true"}
            ],
            valueField:'defaultValue',
            textField:'Desc',

        })

    RuleInfoDlg.initSourceForm();


});
